-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPrefix to Infix.cpp
50 lines (44 loc) · 1.35 KB
/
Prefix to Infix.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#include <iostream>
#include <stack>
#include <algorithm>
using namespace std;
bool isOperand(char data){
return (data >= 'a' && data <= 'z') || (data >= 'A' && data <= 'Z');
}
bool isOperator(char data){
return (data == '+' || data == '-' || data == '*' || data == '/' || data == '^');
}
string infix(string prefix){
// Remove spaces
prefix.erase(remove(prefix.begin(), prefix.end(), ' '), prefix.end());
stack<string> stack;
string A, B, sum;
for(int i = prefix.size()-1; i >= 0; i--){
if(isOperator(prefix[i])){
if(!stack.empty()){
string A, B, sum;
A = stack.top(); stack.pop();
if(stack.empty()){
sum = A + " " + string(1, prefix[i]);
} else {
B = stack.top(); stack.pop();
sum = A + " " +prefix[i] + " " + B;
}
stack.push(sum);
}
} else {
stack.push(string(1, prefix[i]));
}
}
return (!stack.empty() && stack.top() != "!") ? stack.top() : "!";
}
int main(){
string prefix = "++-+KL*MN*//*^OPWUVTQ";
cout << "Prefix: " << prefix << endl;
string result = infix(prefix);
if(result == "!"){
cout << "INPUT: Invalid Expression!" << endl;
} else {
cout << "Infix: " << result << endl;
}
}